// DumpMemoryLeaks.cpp : Defines the entry point for the console application.
//


// VC Samples: CodePlex\CodeForge\Samples\VS2010Samples\VC2010Samples\crt\crt_dbg1

#if 1
//  Case 1:



#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)


int main()
{
    // Sets a breakpoint on a specified object allocation order number (debug version only).
//     _CrtSetBreakAlloc(68);

    int* leak = new int[10];

    int* leak2 = (int*)malloc(1);  
    
    // Dumps all the memory blocks in the debug heap when a memory leak has occurred (debug version only).
    // _CrtDumpMemoryLeaks is frequently called at the end of program execution to verify that all memory allocated by the application has been freed.
    // The function can be called automatically at program termination by turning on the _CRTDBG_LEAK_CHECK_DF bit field of the _crtDbgFlag flag using the _CrtSetDbgFlag function.
    _CrtDumpMemoryLeaks();  

	return 0;
}



#elif 0
//  Case 2:



#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)

#ifdef   _DEBUG
#define  SET_CRT_DEBUG_FIELD(a) \
    _CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#define  CLEAR_CRT_DEBUG_FIELD(a) \
    _CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#else
#define  SET_CRT_DEBUG_FIELD(a)   ((void) 0)
#define  CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
#endif

void detect_memory_leaks( bool on_off )
{
    int flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);

    if(on_off) {
        flags |= _CRTDBG_LEAK_CHECK_DF;
        //_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
        //_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
    }
    else
        flags &= ~_CRTDBG_LEAK_CHECK_DF;

    // If we dump to stdout, we can use _CrtSetDbgFlag()
    _CrtSetDbgFlag( flags );
}

int main()
{
    _CrtSetDbgFlag( _CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) );

    //_CrtSetBreakAlloc(68);

    int* leak = new int[10];

    int* leak2 = (int*)malloc(1);  
    
    return 0;
}




#else
//  Case 3:



#include <Windows.h>

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)


int main()
{
    HANDLE hLogFile ;

    hLogFile = CreateFile(L"d:/memleak.log" ,
                          GENERIC_WRITE,
                          FILE_SHARE_WRITE|FILE_SHARE_READ ,
                          NULL,
                          CREATE_ALWAYS,
                          FILE_ATTRIBUTE_NORMAL,
                          NULL);
    _CrtSetReportMode(_CRT_WARN , _CRTDBG_MODE_FILE);
    _CrtSetReportFile(_CRT_WARN , hLogFile);
    
    // If we use _CrtSetDbgFlag(), nothing dump to the file.
    // _CrtSetDbgFlag( _CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) );

    int* leak = new int[10];
    int* leak2 = (int*)malloc(1);  
    
    // But _CrtDumpMemoryLeaks() will take effect.
    _CrtDumpMemoryLeaks();
    CloseHandle(hLogFile );

	return 0;
}



#endif